$(function(){
     $.ajaxSetup({
         headers:{
             "Authorization":$.cookie("Authorization"),
             "Content-Type":"application/json;charset=UTF-8"
         },
         complete: function(XMLHttpRequest, textStatus) {
         },
         statusCode:{
            404:function pageNotFound(){
                console.log("未找到页面");
            },
            401:function noLogin(){
                apiGoToLoginPage();
            },
            500:function(res){

            }
         }
     });

     $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
     });

      $.extend($.messager.defaults,{
                     ok:"确定",
                     cancel:"取消"
                 });
 });

 // --------------------------------------------------------------------------
 // -----------------------------------------Common--------------------------
 // --------------------------------------------------------------------------
 function messager(title,msg,timeout,icon){
    $.messager.show({
        title:'提示',
        msg:msg,
        showType:'show',
        timeout:4000,
        icon:'info',
        style:{
            left:'',
             right:0,
             top:document.body.scrollTop+document.documentElement.scrollTop,
             bottom:''

        }
     });
 }

function messagerAlert(title,msg,icon){
    $.messager.alert(title,msg,icon);
}

function messagerAlertFn(title,msg,icon,then){
    $.messager.alert(title,msg,icon,then);
}

 function checkValidateForm(ele){
    var result=false;

     ele.form('submit',{
          onSubmit:function(){
              var check=$(this).form('enableValidation').form('validate');
              if(check==true){
                result=true;
              }
              return false;
          }
      });

      return result;
 }

 function getMessagerTip(icon,msg){
    var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/"+icon+".png'/>"+
        "<span style='margin-left:10px'>"+msg+"</span></span>";
    return tip;
 }

function convertFormDataToJsonStr(fields){
    var data="";
    if(fields != null && fields.length>0){
        for(var i=0;i<fields.length;i++){
            var field=fields[i];
            if(i==fields.length-1){
                data+="\""+field.name+"\":\""+field.value+"\"";
            }else{
                data+="\""+field.name+"\":\""+field.value+"\","
            }
        }
    }

    data="{"+data+"}";

    return data;
}

// fields=$("#editDeptForm").find("input").serializeArray();
function convertFormDataToJson(fields){
    var data=convertFormDataToJsonStr(fields);
    var json=JSON.parse(data);
    return json;
}

function JSONToStr(json){
    return JSON.stringify(json)
}

function strToJSON(str){
    return JSON.parse(str)
}
// --------------------------------------------------------------------------
// -----------------------------------------Common--------------------------
// --------------------------------------------------------------------------



// --------------------------------------------------------------------------
// -----------------------------------------Index--------------------------
// --------------------------------------------------------------------------

/*
    跳转至登录页
*/
function apiGoToLoginPage(){
    $.messager.alert('警告','登陆身份过期','error',function(){
        window.top.location.href = "/login";
    });
}

/*
    加载首页左侧树状菜单
*/
function apiIndexMenuTree(then){
    $.get('/baseMenu/tree',then);
}
// --------------------------------------------------------------------------
// -----------------------------------------Index--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------用户管理--------------------------
// --------------------------------------------------------------------------
// 获取用户数据
function apiUserManageGetUserData(then){
    $.get('/eUser/list',then);
}

/*
    添加新用户
*/
function apiUserManageSubmitUserForm(data,then){
    $.post('/eUser/instance',data,then);
}

/*
    编辑新用户
*/
function apiUserManageSubmitEditUserForm(data,then){
    $.ajax({
        url:'/eUser/instance',
        method:'put',
        data:data,
        success:then,
        error: function(xhr,status,error){

        }
    });
}

/*
    删除用户
*/
function apiUserManageRemoveUser(data,then){
    $.ajax({
        url:'/eUser/instance/'+data,
        method:'delete',
        data:data,
        success:then
    });
}

/*
    获取当前用户
*/
function apiUserMangeGetCurrentUser(then){
    $.ajax({
        url: '/eUser/current',
        method:'get',
        success:then
    });
}

// --------------------------------------------------------------------------
// -----------------------------------------用户管理--------------------------
// --------------------------------------------------------------------------


// --------------------------------------------------------------------------
// -----------------------------------------菜单管理--------------------------
// --------------------------------------------------------------------------
// 添加菜单数据
function apiMenuManageAddMenuData(data,then){
    $.post('/baseMenu/instance',data,then);
}

/*
    修改菜单数据
*/
function apiMenuManageEditMenuData(data,then){
     $.ajax({
        url:'/baseMenu/instance',
        method:'put',
        data:data,
        success:then
     });
}

/*
    按ID查询菜单
*/
function apiMenuManageGetById(id,then){
    $.get('/baseMenu/'+id,then);
}

/*
    按ID删除
*/
function apiMenuManageDelById(id,then){
    $.ajax({
        url:'/baseMenu/'+id,
        method:'delete',
        success:then
     });
}
// --------------------------------------------------------------------------
// -----------------------------------------菜单管理--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------权限管理--------------------------
// --------------------------------------------------------------------------
/*
    添加权限
*/
function apiAuthManageAddAuthData(data,then){
    $.ajax({
            url:'/eGroup/instance',
            method:'post',
            data:data,
            success:then
         });
}

/*
    编辑权限
*/
function apiAuthManageEditAuthData(data,then){
    $.ajax({
            url:'/eGroup/instance',
            method:'put',
            data:data,
            success:then
         });
}

/*
    删除权限
*/
function apiAuthManageDelAuthData(id,then){
    $.ajax({
            url:'/eGroup/'+id,
            method:'delete',
            success:then
         });
}

/*
    删除权限
*/
function apiAuthManageGetList(query,then){
    $.ajax({
            url:'/eGroup/'+id,
            method:'get',
            data:query,
            success:then
         });
}

/*
    添加权限与菜单关系
*/
function apiAuthManageAddAuthMenu(data,then){
    $.ajax({
            url:'/eResourceAuthority/authorityMenu?authorityId='+
                data.authorityId+'&resourceId='+data.resourceId,
            method:'post',
            success:then
         });
}

/*
    删除权限与菜单关系
*/
function apiAuthManageDelAuthMenu(data,then){
    $.ajax({
            url:'/eResourceAuthority/'+id,
            method:'post',
            data:data,
            success:then
         });
}

/*
    获取权限与菜单关系
*/
function apiAuthManageGetMenuOfAuth(query,then){
    $.ajax({
            url:'/baseMenu/'+query+'/list',
            method:'get',
            success:then
         });
}

/*
    添加权限与用户关系
*/
function apiAuthManageAddAuthUser(data,then){
    $.ajax({
            url:'/eGroupUser/instance?groupId='+
                data.groupId+'&userId='+data.userId,
            method:'post',
            success:then
         });
}

/*
    获取权限与用户关系
*/
function apiAuthManageGetUserOfAuth(query,then){
    $.ajax({
        url:'/eUser/list/'+query+'/users',
        method:'get',
        success:then
     });
}

/*
    删除权限与用户关系
*/
function apiAuthManageDelAuthUser(data,then){
    $.ajax({
        url:'/eGroupUser/instance?groupId='+
            data.groupId+'&userId='+data.userId,
        method:'delete',
        success:then
    });
}
// --------------------------------------------------------------------------
// -----------------------------------------权限管理--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------管理管理--------------------------
// --------------------------------------------------------------------------
/*
    获取部门树
*/
function apiDeptManageGetTree(then){
    $.ajax({
        url:'/eDepart/tree',
        method:'get',
        success:then
    });
}

/*
    添加单个部门
*/
function apiDeptManageAddDept(data,then){
    $.ajax({
        url:'/eDepart/instance',
        method:'post',
        data:data,
        success:then
    });
}

/*
    获取单个部门
*/
function apiDeptManageGetDept(id,then){
    $.ajax({
        url:'/eDepart/instance/'+id,
        method:'get',
        success:then
    });
}

/*
    编辑单个部门
*/
function apiDeptManageEditDept(data,then){
    $.ajax({
        url:'/eDepart/instance',
        method:'put',
        data:data,
        success:then
    });
}

/*
    删除单个部门
*/
function apiDeptManageDelDept(id,then){
    $.ajax({
        url:'/eDepart/instance/'+id,
        method:'delete',
        success:then
    });
}
// --------------------------------------------------------------------------
// -----------------------------------------管理管理--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------字典管理--------------------------
// --------------------------------------------------------------------------
/*
    添加字典
*/
function apiDictManageSubmitDictForm(data,then){
    $.ajax({
        url:'/evpDict/instance',
        method:'post',
        data:data,
        success:then
    });
}

/*
    编辑字典
*/
function apiDictManageSubmitEditDictForm(data,then){
    $.ajax({
        url:'/evpDict/instance',
        method:'put',
        data:data,
        success:then
    });
}

/*
    apiDictManageRemoveUser
    删除字典
*/
function apiDictManageRemoveDict(id,then){
    $.ajax({
        url:'/evpDict/instance/'+id,
        method:'delete',
        success:then
    });
}
// --------------------------------------------------------------------------
// -----------------------------------------字典管理--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------有害气体监控--------------------------
// --------------------------------------------------------------------------
/*
    获取最新监控数据
*/
function apiHarmfulLastestSituation(then){
    $.ajax({
        url:'/inspectionSituation/lastestSituation',
        method:'get',
        success:then
    });
}
// --------------------------------------------------------------------------
// -----------------------------------------有害气体监控--------------------------
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// -----------------------------------------新闻管理--------------------------
// --------------------------------------------------------------------------
function apiNewsGetById(id,then){
    $.ajax({
        url:'/evpNews/instance/'+id,
        method:'get',
        success:then
    });
}
// --------------------------------------------------------------------------
// -----------------------------------------新闻管理--------------------------
// --------------------------------------------------------------------------
